<?php

class ORM_Finder{
    
    static function find($model, $conditions = null, $fields = '*', $order = null, $limit = null , $file_cache=false) {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        if (!empty($conditions)) {
            $where = "WHERE";
            if (is_array($conditions)) {
                foreach ($conditions as $field => $value) {
                    $where .= " $field = ?";
                    $values[] = $value;
                }
            } else {
                $where .= ' '.$conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        
        is_array($fields) ? $fields = join(',', $fields) : $fields;
        
        if (NUll != $order) {
            $order = 'ORDER BY '.$order;
        }
        if (NULL != $limit) {
            $limit = 'LIMIT '.$limit;
        }

        $table = call_user_func(array($model, 'getTableName'), $conditions);
        $sql    = "SELECT $fields FROM {#prefix#}".$table." $where $order $limit";
        $rs = $conn->query($sql, $values);
        $return = array();
        while ($data = $rs->fetch()) {
            $m = new $model;
            foreach ($data as $key => $d) {
                call_user_func(array($m, 'set'.$key), $d);
            }
            $m->setNew(false);
            $return[] = $m;
        }
        return $return;
    }
    
    /**
     * 检索一条数据
     *
     * @param string $model
     * @param array $conditions
     * @param array $fields
     * @param string $order
     * @return Object
     */
    static function findOne($model, $conditions = null, $fields = '*', $order = null, $file_cache=false) {
        $r = self::find($model, $conditions, $fields, $order, 1, $file_cache=false);
        return isset($r[0])?$r[0]:null;
    }
    
    static function findById($model, $id, $file_cache=false) {
        $r = self::find($model, array('id' => $id), '*', null, 1, $file_cache);
        return $r[0];
    }
    
    static function findCount($model, $conditions = null, $field='*') {
        $conn = ConnectionManager::getConn(call_user_func(array($model, 'getConn')));
        if (!empty($conditions)) {
            //$where = "WHERE";
            if (is_array($conditions)) {
                foreach ($conditions as $f => $value) {
                    $where []= " $f = ?";
                    $values[] = $value;
                }
            } else {
                $where[]= ' '.$conditions;
                $values = array();
            }
        } else {
            $where = '';
            $values = array();
        }
        if(!empty($where)) {
            $where  =  " WHERE ". implode(' and ', $where);
        }
        
        $table = call_user_func(array($model, 'getTableName'), $conditions);
        //echo "SELECT count($field) FROM {#prefix#}".$table." $where";
        $rs = $conn->query("SELECT count($field) FROM {#prefix#}".$table." $where", $values);
        //echo "SELECT count($field) FROM {#prefix#}".$table." $where";
        $return = $rs->fetchColumn();
        return $return;
    }
    
}
